package com.mouliu.base.syn13;

/**
 * Thread线程局部变量
 * Thread测试
 * @author mouliu
 * @create 2018-04-06-下午1:46
 */
public class ThreadLocalTest {
    //打印函数
    static void print(String str){
        //1、1打印当前线程本地内存中的localVariable变量的值
        System.out.println(str+":"+localVariable.get());
        //1.2清除当前线程本地内存中localVariable变量
       // localVariable.remove();
    }
    //(2) 创建ThreadLocal变量
    static ThreadLocal<String> localVariable = new ThreadLocal<>();
    public static void main(String[] args){
        //创建线程one
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                //3、1设置线程one中本地变量的localVariable的值
                localVariable.set("threadOne local variable");
                localVariable.set("sadeesss");
                //3.2调用打印函数
                print("threadOne");
                //3、3打印本地变量值
                System.out.println("threadOne remove after" + ":" +localVariable.get());
            }
        });

        //创建线程two
        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                //4.1 设置线程one中本地变量localVariable的值
                localVariable.set("threadTwo local variable");
                localVariable.set("assaaeee");
                //4.2 调用打印函数
                print("threadTwo");
                //4.3打印本地变量值
                System.out.println("threadTwo remove after" + ":" +localVariable.get());
            }
        });

        threadOne.start();
        threadTwo.start();
    }
}
